package class03;

import java.util.HashMap;
import java.util.TreeMap;

/**
 * @description: 介绍两种刷题常用的数据结构
 *              HashMap：
 *              TreeMap：
 * @time: 2021/7/7 18:19
 * @author: lhy
 */
public class HashMapAndTreeMap {
    static class Node{
        int value;
        public Node(int value){
            this.value = value;
        }
    }
    public static void main(String[] args) {
        HashMap<String,String> hashMap1 = new HashMap<>();
        hashMap1.put("a","aa");
        hashMap1.put("b","bb");
        hashMap1.put("c","cc");
        System.out.println("key(a):" + hashMap1.get("a"));
        System.out.println(hashMap1.containsKey("c"));
        System.out.println(hashMap1.containsValue("cc"));
        hashMap1.remove("a");
        System.out.println("key(a):" + hashMap1.get("a"));
        hashMap1.replace("b","bbb");
        System.out.println("key(b):" + hashMap1.get("b"));

        // HashMap String,Double,Integer,Char,Float等原生类型 按值传递 ??
        String s1 = "s1";
        String s2 = "s1";
        System.out.println(s1 == s2);
        hashMap1.put(s1,"I am S1.");
        System.out.println("key(s1): " + hashMap1.get(s1));
        System.out.println("key(s2): " + hashMap1.get(s2));
        Integer integer1 = 123;
        Integer integer2 = 123;
        System.out.println(integer1 == integer2);

        // 自定义类型传递的是 址
        Node node1 = new Node(123);
        Node node2 = new Node(123);
        HashMap<Node,Integer> hashMap2 = new HashMap<>();
        hashMap2.put(node1,555);
        System.out.println(hashMap2.get(node1));
        System.out.println(hashMap2.get(node2));

        System.out.println("=========================treeMap=========================");
        TreeMap<Integer,String> treeMap = new TreeMap<>();
        treeMap.put(1,"hello1");
        treeMap.put(3,"hello3");
        treeMap.put(5,"hello5");
        treeMap.put(7,"hello7");
        treeMap.put(2,"hello2");
        treeMap.put(4,"hello4");
        System.out.println("key(1): " + treeMap.get(1));
        System.out.println(treeMap.containsKey(1));
        System.out.println(treeMap.containsKey(10));
        treeMap.put(1,"hello11");
        System.out.println("key(1): " + treeMap.get(1));

        System.out.println("--------------treeMap强大之处--------------");
        System.out.println(treeMap.firstKey());
        System.out.println(treeMap.lastKey());
        // <=5 离5最近的key
        System.out.println(treeMap.floorKey(5));
        // <=6 离6最近的key
        System.out.println(treeMap.floorKey(6));
        // >=5 离5最近的key
        System.out.println(treeMap.ceilingKey(5));
        // >=6 离6最近的key
        System.out.println(treeMap.ceilingKey(6));
        TreeMap<Node,Integer> treeMap1 = new TreeMap<>();
        // Exception in thread "main" java.lang.ClassCastException:class03.HashMapAndTreeMap$Node cannot be cast to java.lang.Comparable
        treeMap1.put(node1,666);
        System.out.println(treeMap1.get(node1));
        System.out.println(treeMap1.get(node2));
    }
}
